﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Gao.Poker.App.Helper;
using Gao.Poker.App.Members;
using Gao.Poker.App.Print;
using Gao.Poker.Command;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;
using Gao.Warrior.WPF.Core.Command;

namespace Gao.Poker.App.ViewModel
{
    public class MemberViewModel : BaseViewModel
    {
        #region Command
        public ICommand SaveCommand { get; set; }
        private readonly ICommand _ExportCommand;
        private readonly ICommand _CancelCommand;
        /// <summary>
        /// 导出
        /// </summary>
        public ICommand ExportCommand { get { return _ExportCommand; } }
        /// <summary>
        /// 返回到主菜单
        /// </summary>
        public ICommand CancelCommand { get { return _CancelCommand; } }
        /// <summary>
        /// 查询购买记录
        /// </summary>
        public ICommand SearchConsumptionsCommand { get; private set; }
        /// <summary>
        /// 消费记录查询
        /// </summary>
        public ICommand SearchLogCommand { get; private set; }
        #endregion

        private List<Member> _members;
        private int _memberTotal;
        private int _pageIndex;

        private Member _Member;

        #region View

        /// <summary>
        /// 主显示区
        /// </summary>
        public ContentControl Content { get; set; }

        private MemberView _memberView;

        #endregion
       



        public String MemberName { get; set; }
        public String MemberCardNo { get; set; }
        public List<GameRecord> _membersGameRecords;
        /// <summary>
        /// 销售 开始日期
        /// </summary>
        public DateTime SaleStartDate { get; set; }
        /// <summary>
        /// 销售 结束日期
        /// </summary>
        public DateTime SaleEndDate { get; set; }
        /// <summary>
        /// 消费记录 开始时间
        /// </summary>
        public DateTime LogStartDate { get; set; }
        /// <summary>
        /// 消费记录结束时间
        /// </summary>
        public DateTime LogEndDate { get; set; }

        public List<GameRecord> MembersGameRecords
        {
            get
            {
                _membersGameRecords = new MemberService().GetGameRecord(Member.CardNo);
                return _membersGameRecords;
            }
            set { _membersGameRecords = value; }
        }

        public List<MemberRecord> MemberRecords
        {
            get
            {
                return MemberRecordService.Instance().GetMemberRecord(Member.ID, LogStartDate, LogEndDate);
            }
        }

        public List<Consumption> Consumptions
        {
            get
            {
                return ConsumptionService.Instance().Search(Member.CardNo, SaleStartDate, SaleEndDate);
            }
        }


        public virtual Member Member
        {
            get { return _Member ?? (_Member = new Member()); }
            set
            {
                _Member = value;
                OnPropertyChanged("Member");
                OnPropertyChanged("MembersGameRecords");
                OnPropertyChanged("MemberRecords");
                OnPropertyChanged("Consumptions");
            }
        }

        public MemberViewModel(ContentControl cc)
        {
            Content = cc;
            _memberView = new MemberView();
            MemberName = String.Empty;
            MemberCardNo = String.Empty;

            SaleStartDate = DateTime.Now.AddMonths(-3);
            SaleEndDate = DateTime.Now;

            LogStartDate = DateTime.Now.AddMonths(-3);
            LogEndDate = DateTime.Now;

            SaveCommand = new MemberSaveCommand(new Action<Boolean,Member>(SaveResult));
            _ExportCommand = new RelayCommand(a => OnExport());
            _CancelCommand = new RelayCommand(OnCancel);
            SearchConsumptionsCommand = new RelayCommand(OnSearchConumptions);
            SearchLogCommand = new RelayCommand(OnSearchLog);

        }

        private void OnSearchLog(object obj)
        {
            OnPropertyChanged("MemberRecords");
        }

        private void OnSearchConumptions(object obj)
        {
            OnPropertyChanged("Consumptions");
        }

        public List<Member> Members
        {
            get
            {
                _members = new MemberService().SearchMembers(MemberName, MemberCardNo,PageIndex,20);
                OnPropertyChanged("MemberTotal");
                return _members;
            }
        }

        public int MemberTotal
        {
            get
            {
                _memberTotal = MemberService.Instance().SearchMembersTotal(MemberName, MemberCardNo);
                return _memberTotal;
            }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                OnPropertyChanged("Members");
            }
        }


        #region 方法

        /// <summary>
        /// 取消返回主界面
        /// </summary>
        /// <param name="obj"></param>
        private void OnCancel(object obj)
        {
            Content.Content = _memberView;
        }

        /// <summary>
        /// 保存成功
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="member"></param>
        private void SaveResult(bool obj, Member member)
        {
            if (obj)
            {
                OnPropertyChanged("Members");
                ShowMessage("数据保存成功");
                OnPrintMember(member);
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="obj"></param>
        private void OnExport()
        {
            List<Member> list = MemberService.Instance().SearchMembers();
            DataTable dt = new DataTable();
            dt.Columns.Add("卡号");
            dt.Columns.Add("身份证");
            dt.Columns.Add("姓名");
            dt.Columns.Add("地址");
            dt.Columns.Add("生日");
            dt.Columns.Add("电话");
            dt.Columns.Add("E-MAIL");
            dt.Columns.Add("消费金额");
            dt.Columns.Add("积分");
            dt.Columns.Add("开卡日期");
            dt.Columns.Add("备注");

            foreach (var member in list)
            {
                dt.Rows.Add(member.CardNo,
                            member.IdNo,
                            member.Name,
                            member.Address,
                            member.Birthday.ToString("yyyy-MM-dd"),
                            member.Telephone,
                            member.Email,
                            member.ConsumeMoney,
                            member.Points,
                            member.OpenCardDate.ToString("yyyy-MM-dd"),
                            member.Remark);
            }

            //DataTable dt = DataTableHelper.ListToDataTable(list);

            ExcelHelper.CreateXmlExcelFile(new DataTable[] { dt }, String.Format("会员名单{0}", DateTime.Now.ToString("yyyy-MM-dd")));
        }
      
        /// <summary>
        /// 打印会员
        /// </summary>
        /// <param name="member"></param>
        private void OnPrintMember(Member member)
        {
            PrintHelper.Instance().MemberPrint(member.Name, member.CardNo, member.Points, member.Points);
        }

        #endregion
    }
}
